﻿using ExpenseSystem.Common;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Core.UserModules;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Fether;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Http;
using ExpenseSystem.Core.DataAddOn;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/User")]
    public class UserController : ApiController
    {
        SetInhibitionMaker inhibitionMaker = new UserSetInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new UserGetInhibitionMaker();

        SetInhibition inhibition;
        GetInhibition getInhibition;
        public UserController()
        {
            inhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }
        [Route("WhoAmI")]
        [HttpGet]
        public ResponseModel GetLoginStatus()
        {
            ResponseModel responseModel = new ResponseModel();
            UserProfile userProfile = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
            if(userProfile!=null)
            {
                responseModel.Count = 1;
                responseModel.Data = JsonHelper.ToJson(userProfile, null);
                responseModel.Message = "操作成功";
                responseModel.Result = Definition.RESP_SUCCESS;
                return responseModel;
            }
            else
            {
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "操作失败";
                responseModel.Result = Definition.RESP_LOGIN_FAILD;
            }
            return responseModel;
        }
        [Route("Logout")]
        [HttpGet]
        public ResponseModel Logout()
        {
            UserProfile userProfile = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
            if (userProfile != null)
            {
                LogHelper.Auth(this.GetType().Name,String.Format("用户{0}注销成功",userProfile.UserCode));
            }
            HttpContext.Current.Session.Remove(Definition.SESSION_LOGIN_STR);
            ResponseModel result = new ResponseModel();
            result.Message = "注销成功";
            result.Result = Definition.RESP_SUCCESS;
            result.Count = 0;
            return result;
        }
        [Route("ChgPwd")]
        [HttpGet]
        public ResponseModel ChgPwd(long uid,string password)
        {
            ResponseModel responseModel = new ResponseModel();
            UserManager userManager = UserManager.CreateInstance();
            bool b = userManager.ChangePwd(uid,password);
            if(b)
            { 
                responseModel.Count = 1;
                responseModel.Data =null;
                responseModel.Message = "操作成功";
                responseModel.Result = Definition.RESP_SUCCESS;
                return responseModel;
            }
            else
            {
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "操作失败";
                responseModel.Result = Definition.RESP_LOGIN_FAILD;
            }
            return responseModel;
        }
        [Route("Login")]
        [HttpGet]
        public ResponseModel Login(string user,string password,long site)
        { 
            ResponseModel result = new ResponseModel();
            UserFetcher userFetcher = new UserFetcher();
            if(String.IsNullOrWhiteSpace(user)||String.IsNullOrWhiteSpace(password))
            {
                result.Count = 1;
                result.Result = Definition.RESP_ARG_ERR;
                result.Message = "登录时传入的参数有误";
                result.Data = "";
                LogHelper.Auth(GetType().Name,String.Format("[{0}]Login传入的参数有误",HttpContext.Current.Request.UserHostAddress));
                return result;
            }
            password = StringHelper.StringToMD5(password);
            string message = "";
            UserModel userModel = userFetcher.GetUserByCodeAndPassword( user, password, out message);
            if(userModel==null)
            {
                result.Count = 1;
                result.Result = Definition.RESP_LOGIN_FAILD;
                result.Message = "登录错误";
                result.Data = "";
                LogHelper.Auth(GetType().Name, String.Format("[{0}]用户[{1}]登录错误", HttpContext.Current.Request.UserHostAddress,user));
            }
            else
            {
                UserProfile profile = ProfileConverter.FromUser(userModel,site);
                HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] = profile;
                result.Count = 1;
                result.Result = Definition.RESP_SUCCESS;
                result.Message = "成功";
                result.Data = JsonHelper.ToJson( profile,null);
                UserManager userManager = UserManager.CreateInstance();
                userModel.LastLogin = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
                if(!userManager.Save(userModel, out message))
                {
                    LogHelper.Error(GetType().Name,String.Format("回写最后登录日志失败：{0}",message));
                }
                LogHelper.Auth(GetType().Name, String.Format("[{0}]用户[{1}]登录成功", HttpContext.Current.Request.UserHostAddress, user));
            }
            return result;
        }
        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return SystemControllerHelper.List(new UserFetcher(), "SYS_USER_LIST", RestainConverter.FromRequestArgs(paramModels), getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return SystemControllerHelper.Get(new UserFetcher(), "SYS_USER_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel SetUser(UserModel data)
        {
            return SystemControllerHelper.Set( new UserFetcher(),"SYS_USER_SET", data, UserManager.CreateInstance(), inhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel RemoveUser(UserModel data)
        {
            return SystemControllerHelper.Remove( "SYS_USER_DEL", data, UserManager.CreateInstance());
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveUserById([FromUri]long[] id)
        {
            return SystemControllerHelper.RemoveById("SYS_USER_DEL", id, UserManager.CreateInstance());
        }
    }
}
